﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KardexSist.UserControls.Common;
using Kardex_BussinesLogic;
using Kardex_Common;
using Kardex_Entity;
using KardexSist.Library;
namespace KardexSist.UserControls.SalePlace
{
    public partial class CtrlMainSalePlace : CtrlMainBase, IOperations
    {
        private List<person> ListClient;
        private List<sucursal> ListSucursal;
        private IOperations IOperatioSingle;
        private IOperations IOperationsClient;
        private long idPerson=0;
        public CtrlMainSalePlace()
        {
            InitializeComponent();
            ctrlClientDataPersonal1.MostrarListaClick += LoadGrid;
            IOperationsClient = (IOperations)ctrlClientDataPersonal1;

            ctrlPointSale1.ShowListClick += LoadGridSucursal;
            IOperatioSingle = (IOperations)ctrlPointSale1;
        }
        private void LoadGridSucursal( )
        {
            try
            {
                SucursalFactory d = new SucursalFactory();
                ListSucursal = d.GetListAllByIdPerson(idPerson);
                grdListaSucursales.DataSource = ListSucursal;
                FormatColumnsSucursal();
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }
        private void FormatColumnsSucursal()
        {
            try
            {
               grvListaSucursales.Columns["IdSucursal"].Visible = false;
                grvListaSucursales.Columns["Days"].Visible = false;
                grvListaSucursales.Columns["IdTypeBusiness"].Visible = false;
                grvListaSucursales.Columns["IdPerson"].Visible = false;
                grvListaSucursales.Columns["IdZona"].Visible = false;
                grvListaSucursales.Columns["IdRuta"].Visible = false;
                grvListaSucursales.Columns["NitBusiness"].Visible = false;
                grvListaSucursales.Columns["NameFact"].Visible = false;
                grvListaSucursales.Columns["TelephoneBusiness"].Visible = false;
                grvListaSucursales.Columns["DirectionBusiness"].Visible = false;
                grvListaSucursales.Columns["RefDirectionBusiness"].Visible = false;
                grvListaSucursales.Columns["EnableCredit"].Visible = false;
                grvListaSucursales.Columns["GpsLatitud"].Visible = false;
                grvListaSucursales.Columns["GpsLongitud"].Visible = false;
                grvListaSucursales.Columns["GpsAltura"].Visible = false;
                grvListaSucursales.Columns["GpsDatum"].Visible = false;
                grvListaSucursales.Columns["Coment"].Visible = false;
                grvListaSucursales.Columns["DateRegistInsert"].Visible = false;
                grvListaSucursales.Columns["DateRegistModif"].Visible = false;
                grvListaSucursales.Columns["UserRegisInsert"].Visible = false;
                grvListaSucursales.Columns["UserRegisModif"].Visible = false;
                grvListaSucursales.Columns["client"].Visible = false;
                grvListaSucursales.Columns["typecredit"].Visible = false;
                grvListaSucursales.Columns["typebusiness"].Visible = false;
                grvListaSucursales.Columns["zonestructure"].Visible = false;

            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }
        private void LoadGrid() 
        {
            try
            {
                ClientFactory d = new ClientFactory();
                ListClient = d.GetListClients();
                 grdListClients.DataSource = ListClient;
                 FormatColumns();
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }
        private void FormatColumns()
        {
            grvClients.Columns["IdPerson"].Visible = false;
            grvClients.Columns["centrodistribucion"].Visible = false;
            grvClients.Columns["distribuidor"].Visible = false;
            grvClients.Columns["lugarciexpedido"].Visible = false;
            grvClients.Columns["client"].Visible = false;
            grvClients.Columns["supervisor"].Visible = false;
            grvClients.Columns["lugarciexpedido"].Visible = false;
            grvClients.Columns["preventista"].Visible = false;
            grvClients.Columns["IdTipoCIExpedido"].Visible = false;
            grvClients.Columns["Telephone"].Visible = false;

            grvClients.Columns["Name"].Caption = "Nombre";
            grvClients.Columns["Name"].VisibleIndex = 0;

            grvClients.Columns["CI"].Caption = "CI";
            grvClients.Columns["CI"].VisibleIndex = 1;

            grvClients.Columns["IdCD"].Caption = "Centro Dist.";
            grvClients.Columns["IdCD"].VisibleIndex = 3;

           
        }
        public void Add()
        {
            if (tbListPointSale.SelectedTabPage.Name != "xtabOwners")
            {
                IOperatioSingle.Add();
            }
            else
            {
                IOperationsClient.Add();
            }            
        }

        public void Delete()
        {
            if (tbListPointSale.SelectedTabPage.Name != "xtabOwners")
            {
                IOperatioSingle.Delete();
            }
            else
            {
                IOperationsClient.Delete();
            }            
        }

        public void Save()
        {
            if (tbListPointSale.SelectedTabPage.Name != "xtabOwners")
            {
                IOperatioSingle.Save();
            }
            else
            {
                IOperationsClient.Save();
            }            
        }

        public void Export()
        {
            Utils.ExportGridView(grvClients); 

        }

        private void grvClients_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                object objClient = grvClients.GetFocusedRow();
                if (objClient != null)
                {
                    person personCurrent = new person() { client = new client() };
                    var obj = (person)objClient;
                    personCurrent.IdCD = obj.IdCD;
                    personCurrent.IdPerson = obj.IdPerson;
                    personCurrent.IdTipoCIExpedido = obj.IdTipoCIExpedido;
                    personCurrent.Name = obj.Name;
                    personCurrent.Telephone = obj.Telephone;
                    personCurrent.Activ = obj.Activ;
                    personCurrent.CI = obj.CI;
                    personCurrent.client.IdPerson = obj.IdPerson;
                    personCurrent.client.NameTypeClient = obj.client.NameTypeClient;
                    personCurrent.client.NameTypeStatusCivil = obj.client.NameTypeStatusCivil;
                    personCurrent.client.DateBirth = obj.client.DateBirth;
                    personCurrent.client.Sex = obj.client.Sex;
                    personCurrent.client.DirectionProp = obj.client.DirectionProp;
                    personCurrent.client.RefDirectionProp = obj.client.RefDirectionProp;
                    ctrlClientDataPersonal1.TrigerObjPerson(personCurrent);
                    idPerson = obj.IdPerson;
                    LoadGridSucursal();
                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        private void grvListaSucursales_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                object objSucursal = grvListaSucursales.GetFocusedRow();
                if (objSucursal != null)
                {
                    sucursal SucursalCurrent= new sucursal();
                    var obj = (sucursal)objSucursal;
                    SucursalCurrent.IdSucursal = obj.IdSucursal;
                    SucursalCurrent.IdPerson = obj.IdPerson;
                    SucursalCurrent.NameBusiness = obj.NameBusiness;
                    SucursalCurrent.NameFact = obj.NameFact;
                    SucursalCurrent.IdTypeBusiness = obj.IdTypeBusiness;
                    SucursalCurrent.NitBusiness = obj.NitBusiness;
                    SucursalCurrent.TelephoneBusiness = obj.TelephoneBusiness;
                    SucursalCurrent.DirectionBusiness = obj.DirectionBusiness;
                    SucursalCurrent.RefDirectionBusiness = obj.RefDirectionBusiness;
                    SucursalCurrent.IdZona = obj.IdZona;
                    SucursalCurrent.IdRuta = obj.IdRuta;
                    SucursalCurrent.Days = obj.Days;
                    SucursalCurrent.GpsLatitud = obj.GpsLatitud;
                    SucursalCurrent.GpsLongitud = obj.GpsLongitud;
                    SucursalCurrent.GpsAltura = obj.GpsAltura;
                    SucursalCurrent.GpsDatum = obj.GpsDatum;
                    SucursalCurrent.EnableCredit = obj.EnableCredit;
                    SucursalCurrent.Coment = obj.Coment;                    
                    
                    ctrlPointSale1.TrigerObjSucursal(SucursalCurrent);                    
                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        private void tbListPointSale_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            try
            {
                var PageSelected = e.Page.Name;
                if (PageSelected== "xtabOwners")
                {
                    
                }
                else
                {


                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }
    }
}

